package com.synnex.mobile.servier.web.calendar.controller.admin;

import com.synnex.mobile.generic.core.exception.SystemException;
import com.synnex.mobile.generic.core.util.MultiValueMap;
import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.generic.log.dao.annotation.LogClass;
import com.synnex.mobile.generic.log.dao.annotation.LogMethod;
import com.synnex.mobile.generic.log.entity.LogEntry;
import com.synnex.mobile.generic.web.anotation.mapping.RequestMapping;
import com.synnex.mobile.generic.web.view.ModelAndView;
import com.synnex.mobile.generic.web.view.RedirectView;
import com.synnex.mobile.servier.core.calendar.bo.CalendarBo;
import com.synnex.mobile.servier.web.calendar.controller.AbstractCalendarController;
import com.synnex.mobile.servier.web.calendar.form.CalendarForm;
import com.synnex.mobile.servier.web.utils.DateUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-6
 * To change this template use File | Settings | File Templates.
 */
@RequestMapping(url="/admin/calendar")
@LogClass(name="admin calendar")
public class AdminCalendarController extends AbstractCalendarController {

    @RequestMapping(url="/listCalendar.do",formCommand = RequestMapping.JSON_FORM_COMMAND)
    @LogMethod(name="list calendar")
    public ModelAndView ListCalendar(HttpServletRequest request,HttpServletResponse response,CalendarForm form){
        Map<String,Object> model = new HashMap<String,Object>();
        try {
            List<CalendarBo> data = calendarService.listCalendar(form.getDay());

            String day = request.getParameter("day");
            if(day!=null && !day.equals("")){
                day = DateUtil.dateToStr(DateUtil.strToDate(day,"yyyy-MM-dd"),"yyyy-MM-dd");
            }
                model.put("day",day);
            model.put("totalCount", data.size());
            model.put("results",data);

            return new ModelAndView(model);
        } catch (Exception e) {
            throw  new SystemException(e);
        }
    }

    @RequestMapping(url="/addCalendar.do")
    @LogMethod(name="jump to add calendar page")
    public ModelAndView addCalendar(HttpServletRequest request,HttpServletResponse response,CalendarForm form){

        Map<String,Object> model = new HashMap<String,Object>();
        try {
            return new ModelAndView(model);
        } catch (Exception e) {
            throw  new SystemException(e);
        }
    }

    @RequestMapping(url="/addCalendarAction.do",formCommand="addCalendar")
    @LogMethod(name="add calendar action")
    public ModelAndView addCalendarAction(HttpServletRequest request,HttpServletResponse response,CalendarForm form){
        Map<String,Object> model = new HashMap<String,Object>();
        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        try {

            CalendarBo bo = ObjectUtil.copyProperties(form,CalendarBo.class);

            MultiValueMap url = uploadFilesToFtp(request, "amIconName", null);
            if (null != url.get("amIconName")) {
                String fileName = url.get("amIconName").toString();

                bo.setAmIcon(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url1 = uploadFilesToFtp(request, "amIconTodayName", null);
            if (null != url1.get("amIconTodayName")) {
                String fileName = url1.get("amIconTodayName").toString();

                bo.setAmIconToday(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url2 = uploadFilesToFtp(request, "pmIconName", null);
            if (null != url2.get("pmIconName")) {
                String fileName = url2.get("pmIconName").toString();

                bo.setPmIcon(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url4 = uploadFilesToFtp(request, "pmIconTodayName", null);
            if (null != url4.get("pmIconTodayName")) {
                String fileName = url4.get("pmIconTodayName").toString();

                bo.setPmIconToday(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url3 = uploadFilesToFtp(request, "noonIconName", null);
            if (null != url3.get("noonIconName")) {
                String fileName = url3.get("noonIconName").toString();

                bo.setNoonIcon(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url5 = uploadFilesToFtp(request, "noonIconTodayName", null);
            if (null != url5.get("noonIconTodayName")) {
                String fileName = url5.get("noonIconTodayName").toString();

                bo.setNoonIconToday(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url6 = uploadFilesToFtp(request, "nightIconName", null);
            if (null != url6.get("nightIconName")) {
                String fileName = url6.get("nightIconName").toString();

                bo.setNightIcon(fileName.substring(1, fileName.length() - 1));
            }

            MultiValueMap url7 = uploadFilesToFtp(request, "nightIconTodayName", null);
            if (null != url7.get("nightIconTodayName")) {
                String fileName = url7.get("nightIconTodayName").toString();

                bo.setNightIconToday(fileName.substring(1, fileName.length() - 1));
            }

            calendarService.addCalendar(bo);
            log.setItemText(bo.toString());

            return new ModelAndView(new RedirectView("/admin/calendar/listCalendar.do"),model);
        } catch (Exception e) {
            throw  new SystemException(e);
        }
    }

    @RequestMapping(url="/viewCalendar.do",formCommand = RequestMapping.JSON_FORM_COMMAND)
    @LogMethod(name="jump to edit calendar page")
    public ModelAndView editCalendar(HttpServletRequest request,HttpServletResponse response,CalendarForm form){
        Map<String,Object> model = new HashMap<String,Object>();
        try {
               CalendarBo bo = calendarService.getCalendarBoById(form.getId());
            String link="http://"+this.getConfig().getFtpHost()+this.getConfig().getFtpDownLoadPath()+"/";
                model.put("data",bo);
            model.put("link",link);
            return new ModelAndView(model);
        } catch (Exception e) {
            throw  new SystemException(e);
        }
    }

    @RequestMapping(url="/editCalendarAction.do",formCommand="editCalendar")
    @LogMethod(name="edit calendar action")
    public ModelAndView editCalendarAction(HttpServletRequest request,HttpServletResponse response,CalendarForm form){
        Map<String,Object> model = new HashMap<String,Object>();
        try {

            CalendarBo bo = ObjectUtil.copyProperties(form,CalendarBo.class);

            //上午
            String fileName = null;
            if(form.getAmIcon()!=null && !form.getAmIcon().equals("")){
                fileName = form.getAmIcon().substring(form.getAmIcon().lastIndexOf("/") + 1, form.getAmIcon().lastIndexOf("."));
            }
            MultiValueMap url = uploadFilesToFtp(request, "amIconName", fileName);
            if (null != url.get("amIconName")) {
                String amIcon = url.get("amIconName").toString();
                bo.setAmIcon(amIcon.substring(1, amIcon.length() - 1));
            }

            //上午当前
            String fileName1 = null;
            if(form.getAmIconToday()!=null && !form.getAmIconToday().equals("")){
                fileName1 = form.getAmIconToday().substring(form.getAmIconToday().lastIndexOf("/") + 1, form.getAmIconToday().lastIndexOf("."));
            }
            MultiValueMap url1 = uploadFilesToFtp(request, "amIconTodayName", fileName1);
            if (null != url1.get("amIconTodayName")) {
                String amIconToday = url1.get("amIconTodayName").toString();

                bo.setAmIconToday(amIconToday.substring(1, amIconToday.length() - 1));
            }

            //下午
            String fileName2 = null;
            if(form.getPmIcon()!=null && !form.getPmIcon().equals("")){
                fileName2 = form.getPmIcon().substring(form.getPmIcon().lastIndexOf("/") + 1, form.getPmIcon().lastIndexOf("."));
            }
            MultiValueMap url2 = uploadFilesToFtp(request, "pmIconName", fileName2);
            if (null != url2.get("pmIconName")) {
                String pmIcon = url2.get("pmIconName").toString();

                bo.setPmIcon(pmIcon.substring(1, pmIcon.length() - 1));
            }

            //下午当前
            String fileName4 = null;
            if(form.getPmIconToday()!=null && !form.getPmIconToday().equals("")){
                fileName4 = form.getPmIconToday().substring(form.getPmIconToday().lastIndexOf("/") + 1, form.getPmIconToday().lastIndexOf("."));
            }
            MultiValueMap url4 = uploadFilesToFtp(request, "pmIconTodayName", fileName4);
            if (null != url4.get("pmIconTodayName")) {
                String pmIconToday = url4.get("pmIconTodayName").toString();

                bo.setPmIconToday(pmIconToday.substring(1, pmIconToday.length() - 1));
            }

            //中午
            String fileName3 = null;
            if(form.getNoonIcon()!=null && !form.getNoonIcon().equals("")){
                fileName3 = form.getNoonIcon().substring(form.getNoonIcon().lastIndexOf("/") + 1, form.getNoonIcon().lastIndexOf("."));
            }
            MultiValueMap url3 = uploadFilesToFtp(request, "noonIconName", fileName3);
            if (null != url3.get("noonIconName")) {
                String noonIcon = url3.get("noonIconName").toString();

                bo.setNoonIcon(noonIcon.substring(1, noonIcon.length() - 1));
            }
            //中午当前
            String fileName5 = null;
            if(form.getNoonIconToday()!=null && !form.getNoonIconToday().equals("")){
                fileName5 = form.getNoonIconToday().substring(form.getNoonIconToday().lastIndexOf("/") + 1, form.getNoonIconToday().lastIndexOf("."));
            }
            MultiValueMap url5 = uploadFilesToFtp(request, "noonIconTodayName", fileName5);
            if (null != url5.get("noonIconTodayName")) {
                String noonIconToday = url5.get("noonIconTodayName").toString();

                bo.setNoonIconToday(noonIconToday.substring(1, noonIconToday.length() - 1));
            }

            //晚上
            String fileName6 = null;
            if(form.getNightIcon()!=null && !form.getNightIcon().equals("")){
                fileName6 = form.getNightIcon().substring(form.getNightIcon().lastIndexOf("/") + 1, form.getNightIcon().lastIndexOf("."));
            }
            MultiValueMap url6 = uploadFilesToFtp(request, "nightIconName", fileName6);
            if (null != url6.get("nightIconName")) {
                String nightIcon = url6.get("nightIconName").toString();

                bo.setNightIcon(nightIcon.substring(1, nightIcon.length() - 1));
            }

            //晚上当前
            String fileName7 = null;
            if(form.getNightIconToday()!=null && !form.getNightIconToday().equals("")){
                fileName7 = form.getNightIconToday().substring(form.getNightIconToday().lastIndexOf("/") + 1, form.getNightIconToday().lastIndexOf("."));
            }
            MultiValueMap url7 = uploadFilesToFtp(request, "nightIconTodayName", fileName7);
            if (null != url7.get("nightIconTodayName")) {
                String nightIconToday = url7.get("nightIconTodayName").toString();

                bo.setNightIconToday(nightIconToday.substring(1, nightIconToday.length() - 1));
            }

            LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
            log.setItemText(bo.toString());
            calendarService.editCalendar(bo);

            return new ModelAndView(new RedirectView("/admin/calendar/listCalendar.do"),model);
        } catch (Exception e) {
            throw  new SystemException(e);
        }
    }

    @RequestMapping(url="/deleteCalendar.do")
    @LogMethod(name="delete calendar")
    public  ModelAndView deleteCalendar(HttpServletRequest request,HttpServletResponse response){

        String id = request.getParameter("id");
        calendarService.deleteCalendar(Integer.parseInt(id));

        LogEntry log = this.getAdminSession(request).getCurrentLogEntry(request);
        log.setItemText("delete By id: "+id);

        return new ModelAndView(new RedirectView("/admin/calendar/listCalendar.do"));
    }

}
